//
//  SYProfileViewController.swift
//  SingWeibo
//
//  Created by shiying on 15/12/15.
//  Copyright © 2015年 shiying. All rights reserved.
//

import UIKit
import SDWebImage
import SVProgressHUD

class SYProfileViewController: SYBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
//        cacheLabel.text = "图片缓存："
        
        
    }
    // 读取缓存大小
    func readCacheSize() {
        SDWebImageManager.sharedManager().imageCache.calculateSizeWithCompletionBlock { (a, b) -> Void in
            print("获取到的缓存大小：a:\(a),b: \(b)")
            self.cacheLabel.text = "缓存大小：\(CGFloat(b) / 1000 / 1000.0)M"
        }
    
    }
    
    
    // 准备UI
    func prepareUI() {
        view.addSubview(cacheLabel)
        view.addSubview(cacheButton)
        
        cacheLabel.frame = CGRect(x: 100, y: 100, width: 100, height: 40)
        cacheButton.frame = CGRect(x: 100, y: 150, width: 100, height: 40)
    
    }
    
    // 在异步线程中清除
    func cacheButtonClick() {
        SDWebImageManager.sharedManager().imageCache.clearDiskOnCompletion { () -> Void in
           SVProgressHUD.showSuccessWithStatus("清除缓存成功")
            self.readCacheSize()
        }
    
    }
    
    
    lazy var cacheLabel: UILabel = UILabel()
    
    lazy var cacheButton: UIButton = {
        let button = UIButton()
    
        button.setTitle("清除缓存", forState: UIControlState.Normal)
        button.addTarget(self, action: "cacheButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}
